package com.wx.learn;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Collection使用：保存学生信息
 */
public class collectionDemo2 {
    public static void main(String[] args) {
        // 新建 Collection对象
        Collection collection = new ArrayList();
        collectionDemo2Student s1 = new collectionDemo2Student("zhang", 23);
        collectionDemo2Student s2 = new collectionDemo2Student("zhang2", 20);
        collectionDemo2Student s3 = new collectionDemo2Student("zhang3", 21);
        // 1.添加数据
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        // 2.删除
        // collection.remove(s1);
        // collection.remove(new collectionDemo2Student("zhang2", 20));
        // collection.clear();
        System.out.println(collection);
        // 3.遍历
        // 增强for
        System.out.println("------------增强for遍历---------------");
        for (Object obj: collection) {
            Object s = obj;
            // collectionDemo2Student st = (collectionDemo2Student) obj;
            System.out.println(s.toString());
        }
        System.out.println("-------------迭代遍历--------------");
        Iterator it = collection.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        // 4.判断
        System.out.println("------------判断------------");
        System.out.println(collection.contains(s1));
        System.out.println(collection.isEmpty());
    }
}
